package com.artisan.view;

import javax.swing.JInternalFrame;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.border.EmptyBorder;

import com.artisan.dao.AdminDao;
import com.artisan.dao.StudentDao;
import com.artisan.dao.TeacherDao;
import com.artisan.model.Admin;
import com.artisan.model.Student;
import com.artisan.model.Teacher;
import com.artisan.util.StringUtil;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.ImageIcon;
import java.awt.Font;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class EditPasswordFrm extends JInternalFrame {

	private JPanel contentPane;
	private JPasswordField oldPasswordTextField;
	private JPasswordField newPasswordTextField;
	private JPasswordField confirmPasswordTextField;
	private JLabel currentUserLabel;

	/**
	 * Launch the application.
	 */
//	public static void main(String[] args) {
//		EventQueue.invokeLater(new Runnable() {
//			public void run() {
//				try {
//					EditPasswordFrm frame = new EditPasswordFrm();
//					frame.setVisible(true);
//				} catch (Exception e) {
//					e.printStackTrace();
//				}
//			}
//		});
//	}

	/**
	 * Create the frame.
	 */
	public EditPasswordFrm() {
		setFrameIcon(new ImageIcon(EditPasswordFrm.class.getResource("/com/artisan/image/修改密码.png")));
		setTitle("修改密码");
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));

		setContentPane(contentPane);
		setClosable(true);
		setIconifiable(true);

		JLabel lblNewLabel = new JLabel("原  密  码:");
		lblNewLabel.setFont(new Font("微软雅黑", Font.PLAIN, 14));
		lblNewLabel.setIcon(new ImageIcon(EditPasswordFrm.class.getResource("/com/artisan/image/密码.png")));

		oldPasswordTextField = new JPasswordField();
		oldPasswordTextField.setColumns(10);

		JLabel lblNewLabel_1 = new JLabel("新  密  码:");
		lblNewLabel_1.setIcon(new ImageIcon(EditPasswordFrm.class.getResource("/com/artisan/image/修改密码.png")));
		lblNewLabel_1.setFont(new Font("微软雅黑", Font.PLAIN, 14));

		newPasswordTextField = new JPasswordField();
		newPasswordTextField.setColumns(10);

		JLabel lblNewLabel_2 = new JLabel("确认密码:");
		lblNewLabel_2.setIcon(new ImageIcon(EditPasswordFrm.class.getResource("/com/artisan/image/修改密码.png")));
		lblNewLabel_2.setFont(new Font("微软雅黑", Font.PLAIN, 14));

		confirmPasswordTextField = new JPasswordField();
		confirmPasswordTextField.setColumns(10);

		JButton submitButton = new JButton("确认修改");
		submitButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				submitEdit(e);
			}
		});
		submitButton.setIcon(new ImageIcon(EditPasswordFrm.class.getResource("/com/artisan/image/确认.png")));
		submitButton.setFont(new Font("微软雅黑", Font.PLAIN, 14));

		JButton resetButton = new JButton("重置");
		resetButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent ae) {
				resetValue(ae);
			}
		});
		resetButton.setIcon(new ImageIcon(EditPasswordFrm.class.getResource("/com/artisan/image/重置.png")));
		resetButton.setFont(new Font("微软雅黑", Font.PLAIN, 14));

		JLabel lblNewLabel_3 = new JLabel("当前用户:");
		lblNewLabel_3.setIcon(new ImageIcon(EditPasswordFrm.class.getResource("/com/artisan/image/用户名.png")));
		lblNewLabel_3.setFont(new Font("微软雅黑", Font.PLAIN, 14));

		currentUserLabel = new JLabel("");
		currentUserLabel.setFont(new Font("微软雅黑", Font.PLAIN, 14));
		GroupLayout gl_contentPane = new GroupLayout(contentPane);
		gl_contentPane.setHorizontalGroup(gl_contentPane.createParallelGroup(Alignment.LEADING).addGroup(gl_contentPane
				.createSequentialGroup()
				.addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING).addGroup(gl_contentPane
						.createSequentialGroup().addGap(65)
						.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
								.addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING, false)
										.addComponent(lblNewLabel_1, Alignment.LEADING, GroupLayout.DEFAULT_SIZE,
												GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
										.addComponent(lblNewLabel, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 89,
												Short.MAX_VALUE)
										.addComponent(lblNewLabel_3, Alignment.LEADING, GroupLayout.DEFAULT_SIZE,
												GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
								.addComponent(lblNewLabel_2, GroupLayout.PREFERRED_SIZE, 103,
										GroupLayout.PREFERRED_SIZE))
						.addPreferredGap(ComponentPlacement.RELATED)
						.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING, false)
								.addComponent(currentUserLabel, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE,
										Short.MAX_VALUE)
								.addComponent(confirmPasswordTextField).addComponent(newPasswordTextField)
								.addComponent(oldPasswordTextField, GroupLayout.DEFAULT_SIZE, 161, Short.MAX_VALUE)))
						.addGroup(gl_contentPane.createSequentialGroup().addGap(99).addComponent(submitButton)
								.addPreferredGap(ComponentPlacement.RELATED, 26, Short.MAX_VALUE)
								.addComponent(resetButton, GroupLayout.PREFERRED_SIZE, 109, GroupLayout.PREFERRED_SIZE)
								.addPreferredGap(ComponentPlacement.RELATED)))
				.addContainerGap(93, GroupLayout.PREFERRED_SIZE)));
		gl_contentPane
				.setVerticalGroup(
						gl_contentPane.createParallelGroup(Alignment.LEADING)
								.addGroup(
										gl_contentPane.createSequentialGroup().addGap(34)
												.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
														.addComponent(lblNewLabel_3).addComponent(currentUserLabel))
												.addGap(18)
												.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
														.addComponent(lblNewLabel, GroupLayout.PREFERRED_SIZE, 26,
																GroupLayout.PREFERRED_SIZE)
														.addComponent(oldPasswordTextField, GroupLayout.PREFERRED_SIZE,
																GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
												.addGap(18)
												.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
														.addComponent(lblNewLabel_1)
														.addComponent(newPasswordTextField, GroupLayout.PREFERRED_SIZE,
																GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
												.addGap(18)
												.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
														.addComponent(lblNewLabel_2).addComponent(
																confirmPasswordTextField, GroupLayout.PREFERRED_SIZE,
																GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
												.addGap(28)
												.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
														.addComponent(submitButton, GroupLayout.DEFAULT_SIZE, 29,
																Short.MAX_VALUE)
														.addComponent(resetButton, GroupLayout.DEFAULT_SIZE,
																GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
												.addGap(23)));
		contentPane.setLayout(gl_contentPane);
		if ("系统管理员".equals(MainFrm.userType.getName())) {
			Admin admin = (Admin) MainFrm.userObject;
			currentUserLabel.setText("【系统管理员】" + admin.getName());
		} else if ("学生".equals(MainFrm.userType.getName())) {
			Student student = (Student) MainFrm.userObject;
			currentUserLabel.setText("【学生】" + student.getName());
		} else {
			Teacher teacher = (Teacher) MainFrm.userObject;
			currentUserLabel.setText("【教师】" + teacher.getName());
		}
	}

	protected void submitEdit(ActionEvent e) {
		// TODO 自动生成的方法存根
		String oldPassword = String.valueOf(oldPasswordTextField.getPassword());
		String newPassword = String.valueOf(newPasswordTextField.getPassword());
		String confirmPassword = String.valueOf(confirmPasswordTextField.getPassword());
		if (StringUtil.isEmpty(oldPassword)) {
			JOptionPane.showMessageDialog(this, "请填写旧密码!");
			return;
		}
		if (StringUtil.isEmpty(newPassword)) {
			JOptionPane.showMessageDialog(this, "请填写新密码!");
			return;
		}
		if (StringUtil.isEmpty(confirmPassword)) {
			JOptionPane.showMessageDialog(this, "请填写新密码!");
			return;
		}
		if (!newPassword.equals(confirmPassword)) {
			JOptionPane.showMessageDialog(this, "两次密码输入不一致,请重新输入!");
			return;
		}
		if ("系统管理员".equals(MainFrm.userType.getName())) {
			AdminDao adminDao = new AdminDao();
			Admin adminTmp = new Admin();
			Admin admin = (Admin) MainFrm.userObject;
			adminTmp.setName(admin.getName());
			adminTmp.setId(admin.getId());
			adminTmp.setPassword(oldPassword);
			JOptionPane.showMessageDialog(this, adminDao.editPassword(adminTmp, newPassword));
			adminDao.closeDao();
			this.dispose();
			return;
		}
		if ("学生".equals(MainFrm.userType.getName())) {
			StudentDao studentDao = new StudentDao();
			Student studentTmp = new Student();
			Student student = (Student) MainFrm.userObject;
			studentTmp.setName(student.getName());
			studentTmp.setPassword(oldPassword);
			studentTmp.setId(student.getId());
			JOptionPane.showMessageDialog(this, studentDao.editPassword(studentTmp, newPassword));
			studentDao.closeDao();
			this.dispose();
			return;
		}
		if ("教师".equals(MainFrm.userType.getName())) {
			TeacherDao teacherDao = new TeacherDao();
			Teacher teacherTmp = new Teacher();
			Teacher teacher = (Teacher) MainFrm.userObject;
			teacherTmp.setName(teacher.getName());
			teacherTmp.setPassword(oldPassword);
			teacherTmp.setId(teacher.getId());
			JOptionPane.showMessageDialog(this, teacherDao.editPassword(teacherTmp, newPassword));
			teacherDao.closeDao();
			this.dispose();
			return;
		}
	}

	protected void resetValue(ActionEvent ae) {
		// TODO 自动生成的方法存根
		oldPasswordTextField.setText("");
		newPasswordTextField.setText("");
		confirmPasswordTextField.setText("");
	}
}
